/**
 * Created by baiguiren on 1/23/17.
 */

let async = require('async');
let _ = require('underscore');

let express = require('express');
let app = express();
// Requires lodash (or underscore), express3 and dresende's orm2
// Part of an app, that fetches cats of the logged user.
// This example uses `seq` function to avoid overnesting and error
// handling clutter
app.get('/cats', function (request, response) {
  let User = request.models.User;
  async.seq(
    _.bind(User.get, User), // 'User.get' has signature (id, callback(err, data))
    function (user, fn) {
      user.getCats(fn);
    }
  )(req.session.user_id, function (err, cats) {
    if (err) {
      console.error(err);
      response.json({status: 'error', message: err.message});
    } else {
      response.json({status: 'ok', message: 'Cats found', data: cats});
    }
  });
});